<?php
namespace app\admin\controller;
use app\adminmall\model\SiteModel;
use app\common\controller\AdminBaseController;
use think\Cache;

/**
 * 系统配置
 * @author Kaisa
 * @version 2017-02-09
 */
class SystemController extends AdminBaseController {
	
	protected $model;
	
    public function _initialize() {
        parent::_initialize();
		$this->model = new SiteModel();
    }

    /**
     * 站点配置
     */
    public function site() {
        if (request()->isPost()) {
            $site_config   = request()->post('site_config/a');
			$site_config['app_debug'] = input('app_debug') ? true : false;
			$site_config['app_trace'] = input('app_trace') ? true : false;
            $data['value'] = serialize($site_config);
            if (db('system')->where('name', 'site_config')->update($data) !== false) {
                cache('site_config', $site_config);
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
        	if (cache('site_config')) {
            $site_config = cache('site_config');
        	} else {
            	$site_config = db('system')->field('value')->where('name', 'site_config')->find();
            	$site_config = unserialize($site_config['value']);
        	}
        	return $this->fetch('site', ['site_config' => $site_config]);
        }
    }
	
	/**
    * 短信配置
    */
    public function sms() {
    	if (request()->isPost()) {
            $sms_config   = request()->post('sms_config/a');
            $data['value'] = serialize($sms_config);
            if (db('system')->where('name', 'sms_config')->update($data) !== false) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
            $sms_config = db('system')->field('value')->where('name', 'sms_config')->find();
            $sms_config = unserialize($sms_config['value']);
        	return $this->fetch('sms', ['sms_config' => $sms_config]);
        }
    }
	
	/**
    * 支付配置
    */
    public function payment() {
    	if (request()->isPost()) {
            $payment_config = request()->post('payment_config/a');
			$payment_config['alipay_status'] = input('alipay_status') ? true : false;
			$payment_config['wxpay_status'] = input('wxpay_status') ? true : false;
			$payment_config['unionpay_status'] = input('unionpay_status') ? true : false;
            $data['value'] = serialize($payment_config);
            if (db('system')->where('name', 'payment_config')->update($data) !== false) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
            $payment_config = db('system')->field('value')->where('name', 'payment_config')->find();
            $payment_config = unserialize($payment_config['value']);
        	return $this->fetch('payment', ['payment_config' => $payment_config]);
        }
    }
	
	/**
    * 日志配置
    */
    public function web_log() {
    	if (request()->isPost()) {
    		$weblog_on=input('weblog_on') ? true : false;
			//设置tags
			$configs = include APP_PATH.'tags.php';
			$module_init = $configs['module_init'];
			if($weblog_on){
				if(!in_array('app\\common\\behavior\\WebLog',$module_init)){
					$module_init[]='app\\common\\behavior\\WebLog';
				}
			}else{
				$key = array_search('app\\common\\behavior\\WebLog', $module_init);
				if($key!==false){
					unset($module_init[$key]);
				}
			}
			$configs=array_merge($configs,['module_init'=>$module_init]);
			file_put_contents(APP_PATH.'tags.php', "<?php\treturn " . var_export($configs, true) . ";");
			$web_log['weblog_on']=$weblog_on;
			$web_log['not_log_module']=input('not_log_module/a');
			$web_log['not_log_controller']=input('not_log_controller/a');
			$web_log['not_log_action']=input('not_log_action/a');
			$web_log['not_log_data']=input('not_log_data/a');
			$web_log['not_log_request_method']=input('not_log_request_method/a');
			$rst=sys_config_setbykey('web_log',$web_log);
			if($rst){
				Cache::clear();
				$this->success('设置保存成功');
			}else{
				$this->error('设置保存失败');
			}
        }else{
        	$web_log = config('log.web_log');
			//模块
			$web_log['not_log_module'] = (isset($web_log['not_log_module']) && $web_log['not_log_module'])?join(',',$web_log['not_log_module']):'';
			$web_log['not_log_controller'] = (isset($web_log['not_log_controller']) && $web_log['not_log_controller'])?join(',',$web_log['not_log_controller']):'';
			$web_log['not_log_action'] = (isset($web_log['not_log_action']) && $web_log['not_log_action'])?join(',',$web_log['not_log_action']):'';
			$web_log['not_log_data'] = (isset($web_log['not_log_data']) && $web_log['not_log_data'])?join(',',$web_log['not_log_data']):'';
			$web_log['not_log_request_method'] = (isset($web_log['not_log_request_method']) && $web_log['not_log_request_method'])?join(',',$web_log['not_log_request_method']):'';
			//控制器 模块
			$controllers = array();
			$actions = array();
			$modules = ['index','admin','adminmall','api'];
			foreach($modules as $module){
				$arr = cache('controllers_'.$module);
				if(empty($arr)){
					$arr = \ReadClass::readDir(APP_PATH . $module. DS .'controller');
					cache('controllers'.'_'.$module,$arr);
				}
				foreach($arr as $key=>$v){
					$controllers[$module][] = $module.'/'.$key;
					$actions[$module.'/'.$key] = array_map('array_shift',$v['method']);
				}
			}
			$methods = ['GET','POST','PUT','DELETE','HEAD','PATCH','OPTIONS','Ajax','Pjax'];
			$this->assign('methods',$methods);
			$this->assign('actions',$actions);
			$this->assign('modules',$modules);
			$this->assign('controllers',$controllers);
			$this->assign('web_log',$web_log);
			return $this->fetch();
        }
    }

/**
    * 清除缓存
    */
    public function clear() {
        if (delete_dir_file(CACHE_PATH) || delete_dir_file(TEMP_PATH)) {
            $this->success('清除缓存成功');
        } else {
            $this->error('清除缓存失败');
        }
    }

	/**
    * 微信配置
    */
    public function wechat() {
    	if (request()->isPost()) {
            $wechat_config = request()->post('wechat_config/a');
            $data['value'] = serialize($wechat_config);
            if (db('system')->where('name', 'wechat_config')->update($data) !== false) {
                $this->success('保存成功');
            } else {
                $this->error('保存失败');
            }
        }else{
            $wechat_config = db('system')->field('value')->where('name', 'wechat_config')->find();
            $wechat_config = unserialize($wechat_config['value']);
        	return $this->fetch('wechat', ['wechat_config' => $wechat_config]);
        }
    }
	
	 /**
     * 商城分销设置
     * @author [lx] 
     */
    public function distribution(){
        $site=$this->model->where("key","distribution")->find();
        if(request()->isAjax()){
        	$param=input('post.');
			$return =$this->model->setting_save($param, "distribution","distribution");
			if ($return) {
				if (cache('setting')) {
					$setting=cache('setting');
				}
				$setting['distribution']=$this->model->where("key","distribution")->find();
				cache("setting",$setting);
             	 $this->success('保存成功', url('system/distribution'));
            } else {
                $this->error('保存失败');
            }
        }
        return $this->fetch('distribution', ['settings' => iunserializer($site['value'])]);
    }

}
